package com.xdtech.play.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xdtech.common.utils.DateUtil;
import com.xdtech.common.utils.DateUtil.DateStyle;
import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.play.dao.PlayStartDao;
import com.xdtech.play.model.PlayMember;
import com.xdtech.play.model.PlayStart;
import com.xdtech.play.service.PlayMemberService;
import com.xdtech.play.service.PlayStartService;
import com.xdtech.play.service.PlayTradeRecordService;
import com.xdtech.play.vo.PlayStartItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2016-05-09 23:28:30
 * @since 1.0
 * @see
 */
@Service
public class PlayStartServiceImpl implements PlayStartService {
	private Log log = LogFactory.getLog(PlayStartServiceImpl.class);
	@Autowired
	private PlayStartDao playStartDao;
	@Autowired
	private BaseDao baseDao;
	@Autowired
	private PlayMemberService playMemberService;
	@Autowired
	private PlayTradeRecordService playTradeRecordService;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-09 23:28:30
	 * @modified by
	 * @param entity
	 */
	public void save(PlayStart entity) {
		playStartDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-09 23:28:30
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<PlayStart> entities) {
		playStartDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-09 23:28:30
	 * @modified by
	 * @param entity
	 */
	public void delete(PlayStart entity) {
		playStartDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-09 23:28:30
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		playStartDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-09 23:28:30
	 * @modified by
	 * @param id
	 * @return
	 */
	public PlayStart get(Long id) {
		return playStartDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-09 23:28:30
	 * @modified by
	 * @return
	 */
	public List<PlayStart> getAll() {
		return playStartDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-09 23:28:30
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> playStarts = null;
		long rows = 0;
		if (pg!=null) {
			Page<PlayStart> page = playStartDao.findPage(pg,"from PlayStart order by createTime desc", values);
			playStarts = BeanUtils.copyListProperties(
					PlayStartItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<PlayStart> playStartList = playStartDao.find("from PlayStart order by id desc", values);
			playStarts = BeanUtils.copyListProperties(
					PlayStartItem.class, playStartList);
			rows = playStarts.size();
		}
		results.put("total", rows);
		results.put("rows", playStarts);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-09 23:28:30
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdatePlayStart(PlayStartItem item) {
		PlayStart playStart = null;
		if (item.getId()==null) {
			playStart = new PlayStart();
		}else {
			playStart = playStartDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(playStart, item);
		playStartDao.save(playStart);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-09 23:28:30
	 * @modified by
	 * @param playStartId
	 * @return
	 */
	public PlayStartItem loadPlayStartItem(Long playStartId) {
		PlayStart playStart = playStartDao.get(playStartId);
		return PlayStartItem.createItem(playStart);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-09 23:28:30
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deletePlayStartInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-09 23:28:30
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deletePlayStartInfo(List<Long> playStartIds) {
		for (Long id : playStartIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-09 23:28:30
	 * @modified by
	 * @return
	 */
	@Override
	public List<PlayStartItem> loadItems() {
		List<PlayStart> playStarts = getAll();
		return PlayStartItem.createItems(playStarts);
	}
	@Override
	public ResultMessage saveStartInfo(PlayStartItem item) {
		ResultMessage rs = new ResultMessage();
		PlayMember member = playMemberService.get(item.getMemberId());
		if (member.getMoney()<item.getAccount()) {
			rs.setFailMsg("金额不足");
		}else {
			PlayStart playStart = new PlayStart();
			playStart.setOperator(item.getOperator());
			playStart.setStartTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
			playStart.setMemberId(member.getId());
			playStart.setMemberName(member.getName());
			playStart.setAccount(item.getAccount());
			playStartDao.save(playStart);
			//变更玩家剩余金额和创建开庄记录
			playTradeRecordService.createPlayStartRecord(member,item.getAccount(),item.getOperator());
		}
		return rs;
	}

}
